package cn.uniquesoft.controller;

import cn.uniquesoft.service.ISearchService;
import cn.uniquesoft.vo.AnswerVO;
import cn.uniquesoft.vo.SearchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2018-11-12 11:16:40
 * @description:
 **/
@Controller
@RequestMapping(value = "/search")
public class SearchController {

    @Autowired
    private ISearchService searchService;

    @RequestMapping(value = "/save")
    @ResponseBody
    public AnswerVO save(SearchVO item) {
        item.setId(item.getTid() + "_" + item.getTname());
        searchService.save(item);
        return AnswerVO.success("success");
    }

    @RequestMapping(value = "/edit")
    @ResponseBody
    public AnswerVO edit(String tid, String tname, String tvalue) {
        searchService.edit(tid, tname, tvalue);
        return AnswerVO.success("success");
    }

    @RequestMapping(value = "/removeById")
    @ResponseBody
    public AnswerVO removeById(String tid, String tname) {
        searchService.remove(tid, tname);
        return AnswerVO.success("success");
    }

    @RequestMapping(value = "/pageQuery")
    @ResponseBody
    public List<SearchVO> pageQuery(String tvalue,int start) {
        System.out.println(tvalue);
        return this.searchService.query(tvalue,start);
    }

    @RequestMapping(value = "/testQuery")
    @ResponseBody
    public List<SearchVO> testQuery(String tvalue, int start) {
        List<SearchVO> items = new ArrayList<>();
        for (int i = start; i < (start + 15); i++) {
            SearchVO item = new SearchVO();
            item.setId("" + i);
            item.setTid(i + "_test");
            item.setTname("test");
            item.setTvalue(tvalue + "" + i);
            items.add(item);
        }
        return items;
    }
}
